﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bass.Tools;
using MySql.Data.MySqlClient;

public partial class admin_admin_notice_list : System.Web.UI.Page
{
    
    DataSet dset = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["adminname"]) == "")
        {
            Response.Write("<script language=javascript>alert('您没有登录');window.parent.document.location.href ='admin_login.aspx'</script>");
        }
        else if (Convert.ToString(Session["adminkind"]) != "product")
        {
            Response.Write("<script language=javascript>alert('您不是产品管理员，无权访问此页！');location='admin_login.aspx'</script>");
        }

        if (!IsPostBack)
        {
            dsdatabind();
        }
    }
    protected void dsdatabind()//绑定GridView1数据
    {
        //OleDbConnection Conn = new OleDbConnection();
        //Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("../miumiu.mdb");
        //Conn.Open();

        //取到连接字符串
        string propertiesPath = Server.MapPath("../mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        string newque = "SELECT * FROM notice_info order by addtime desc";
        MySqlDataAdapter datap = new MySqlDataAdapter(newque, myConnection);
        datap.Fill(dset);
        GridView1.DataSource = dset;
        this.GridView1.DataKeyNames = new string[] { "noticeid" };
        GridView1.DataBind();
        myConnection.Close();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.dsdatabind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)(e.Row.Cells[4].Controls[0])).Attributes.Add("onclick", "return confirm('确定删除吗？')");
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //OleDbConnection Conn = new OleDbConnection();
        //Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("../miumiu.mdb");
        //Conn.Open();

        //取到连接字符串
        string propertiesPath = Server.MapPath("../mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        MySqlCommand comm = new MySqlCommand("delete from notice_info where noticeid='" + id + "'", myConnection);
        comm.ExecuteNonQuery();
        myConnection.Close();
        dsdatabind();
        Response.Write("<script language=javascript>alert('操作成功');location='admin_notice_list.aspx'</script>");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        dsdatabind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        dsdatabind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //OleDbConnection Conn = new OleDbConnection();
        //Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("../miumiu.mdb");
        //Conn.Open();

        //取到连接字符串
        string propertiesPath = Server.MapPath("../mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        string uid = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        GridViewRow row = this.GridView1.Rows[e.RowIndex];
        string ingo = "update notice_info set title='" + ((TextBox)(row.Cells[0].Controls[0])).Text.ToString() + "',content='" + ((TextBox)(row.Cells[1].Controls[0])).Text.ToString() + "' where noticeid=" + uid;
        MySqlCommand goodsin = new MySqlCommand(ingo, myConnection);
        goodsin.ExecuteNonQuery();
        myConnection.Close();
        GridView1.EditIndex = -1;
        dsdatabind();
    }

}
